﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Game.geodata;
using Game.templates.spawn;
using Game.utils;
using lineage2.commons.util;

namespace Game.ai
{
    public class WalkerAI:DefaultAI
    {
        private int _routeIndex = 0;
        /**
         * Field _direction.
         */
        private short _direction = 1;
        /**
         * Field _lastMove.
         */
        private long _lastMove = 0;
        public WalkerAI(NpcInstance actor):base(actor)
        {
            //super(actor);
            setIntention(CtrlIntention.AI_INTENTION_ACTIVE);
        }
        protected override bool thinkActive()
        {
            WalkerRouteTemplate routeTemplate = getActor().getWalkerRouteTemplate();
            if (routeTemplate == null)
            {
                return false;
            }
            bool LINEAR = (routeTemplate.getRouteType() == WalkerRouteTemplate.RouteType.LINEAR);
            bool CYCLE = (routeTemplate.getRouteType() == WalkerRouteTemplate.RouteType.CYCLE);
            bool TELEPORT = (routeTemplate.getRouteType() == WalkerRouteTemplate.RouteType.TELEPORT);
            bool RANDOM = (routeTemplate.getRouteType() == WalkerRouteTemplate.RouteType.RANDOM);
            if (routeTemplate.getIsRunning())
            {
                getActor().setRunning();
            }
            int pointsCount = routeTemplate.getPointsCount();
            if (pointsCount <= 0)
            {
                return false;
            }
            WalkerRouteTemplate.Route point = null;
            int oldIndex = _routeIndex;
            if (((_routeIndex + _direction) >= pointsCount) || ((_routeIndex + _direction) < 0))
            {
                if (LINEAR)
                {
                    _direction *= -1;
                    _routeIndex += _direction;
                    point = routeTemplate.getPoints().get(_routeIndex) as WalkerRouteTemplate.Route;
                }
                else if (CYCLE)
                {
                    _direction = 1;
                    _routeIndex = 0;
                    point = routeTemplate.getPoints().get(_routeIndex) as WalkerRouteTemplate.Route;
                }
                else if (TELEPORT)
                {
                    _direction = 1;
                    _routeIndex = 0;
                    point = routeTemplate.getPoints().get(_routeIndex) as WalkerRouteTemplate.Route;
                }
                else if (RANDOM)
                {
                    randomWalk();
                }
            }
            else
            {
                _routeIndex += _direction;
                point = routeTemplate.getPoints().get(_routeIndex) as WalkerRouteTemplate.Route;
            }
            Location nextLoc = point.getLoc();
            long delay = (point.getDelay() <= 0) ? routeTemplate.getDelay() : point.getDelay();
            if (_lastMove == 0)
            {
                _lastMove = java.lang.System.currentTimeMillis() + delay;
                _routeIndex = oldIndex;
                return false;
            }
            else if (getActor().isMoving)
            {
                _routeIndex = oldIndex;
                return false;
            }
            else if (RANDOM)
            {
                return false;
            }
            else if ((java.lang.System.currentTimeMillis() - _lastMove) > delay)
            {
                getActor().moveToLocation(nextLoc, 0, true);
                _lastMove = java.lang.System.currentTimeMillis();
                if (TELEPORT & point.getLastPoint())
                {
                    getActor().teleToLocation(nextLoc);
                    _lastMove = java.lang.System.currentTimeMillis();
                }
            }
            return true;
        }
        protected override bool randomWalk()
        {
            WalkerRouteTemplate routeTemplate = getActor().getWalkerRouteTemplate();
            int AI_WALK_RANGE = routeTemplate.getWalkRange();
            if ((AI_WALK_RANGE == 0) | (routeTemplate.getRouteType() == WalkerRouteTemplate.RouteType.RANDOM))
            {
                return false;
            }
            NpcInstance actor = getActor();
            if (actor == null)
            {
                return false;
            }
            Location sloc = actor.getSpawnedLoc();
            int x = (sloc.x + Rnd.get(2 * AI_WALK_RANGE)) - AI_WALK_RANGE;
            int y = (sloc.y + Rnd.get(2 * AI_WALK_RANGE)) - AI_WALK_RANGE;
            int z = GeoEngine.getHeight(x, y, sloc.z, actor.getGeoIndex());
            actor.setRunning();
            actor.moveToLocation(x, y, z, 0, true);
            return true;
        }
    }
}
